package mysql

import (
	"github.com/zeromicro/go-zero/core/logx"
	"strings"
)

// CreateHouse 创建房源
func CreateHouse(house House) House {
	h := House{}
	db.Where("title", house.Title).Find(&h)
	if h.ID != 0 {
		return House{}
	}
	db.Create(&house)
	return house
}

// GetList 获取房源列表
func GetList(houseid int) House {
	H := House{}
	db.Table("houses").Select("houses.user_id,houses.title,houses.price,houses.address,houses.room_count,houses.acreage,houses.unit,houses.cover_url,house_facilities.name").Joins("JOIN house_facilities ON houses.id=house_facilities.id").Where("houses.id", houseid).Scan(&H)
	if H.ID == 0 {
		return House{}
	}
	return H
}

// GetFacilityName 获取设施名字
func GetFacilityName(Facility Facility) string {
	db.First(&Facility)
	if Facility.Name == "" {
		logx.Error("没有该设施")
	}
	return Facility.Name
}

// CreateHouseFacility 添加房源设施
func CreateHouseFacility(HouseFacility HouseFacility) HouseFacility {
	db.Create(&HouseFacility)
	return HouseFacility
}

// GetHouseFacility 获取房源设施列表
func GetHouseFacility(houseId int64) []*HouseFacility {
	var result []*HouseFacility
	db.Where("house_id = ?", houseId).Find(&result)
	return result
}

// GetHouseList 房源列表
func GetHouseList(title string, acreage int64, roomCount int64) []House {
	titles := strings.TrimSpace(title)
	h := []House{}
	if len(titles) == 0 {
		db.Find(&h)
		return h
	}
	db.Where("title LIKE ?", "%"+title+"%").Find(&h)
	return h
}
